package me.zhengjie.config;

import me.zhengjie.modules.system.domain.Dept;
import me.zhengjie.modules.system.service.DeptService;
import me.zhengjie.modules.system.service.RoleService;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.modules.system.service.dto.RoleSmallDto;
import me.zhengjie.modules.system.service.dto.UserDto;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Description: 数据权限配置 .<br>
 *
 * @author jm
 * @since 2020/1/20
 */
@Component
public class DataScope {
    private final String[] scopeType = {
        "全部",
        "本级",
        "自定义"
    };
    @Autowired private UserService userService;
    @Autowired private RoleService roleService;
    @Autowired private DeptService deptService;

    public Set<Long> getDeptIds() {
        UserDto user = userService.findByName(SecurityUtils.getUsername());
        // 用于存储部门id
        Set<Long> deptIds = new HashSet<>();
        // 查询用户角色
        List<RoleSmallDto> roleSet = roleService.findByUsersId(user.getId());
        for (RoleSmallDto role : roleSet) {
            if (scopeType[0].equals(role.getDataScope())) {
                return new HashSet<>();
            }
            // 存储本级的数据权限
            if (scopeType[1].equals(role.getDataScope())) {
                deptIds.add(user.getDept().getId());
            }
            // 存储自定义的数据权限
            if (scopeType[2].equals(role.getDataScope())) {
                Set<Dept> depts = deptService.findByRoleIds(role.getId());
                for (Dept dept : depts) {
                    deptIds.add(dept.getId());
                    List<Dept> deptChildren = deptService.findByPid(dept.getId());
                    if (deptChildren != null && deptChildren.size() != 0) {
                        deptIds.addAll(getDeptChildren(deptChildren));
                    }
                }
            }
        }
        return deptIds;
    }


    public List<Long> getDeptChildren(List<Dept> deptList) {
        List<Long> list = new ArrayList<>();
        deptList.forEach(dept -> {
                if (dept != null && dept.getEnabled()) {
                    List<Dept> depts = deptService.findByPid(dept.getId());
                    if (deptList.size() != 0) {
                        list.addAll(getDeptChildren(depts));
                    }
                    list.add(dept.getId());
                }
            }
        );
        return list;
    }
}
